package cn.darkhorse.device.service.impl;

import cn.darkhorse.device.pojo.MesDeviceConsumableInfo;
import cn.darkhorse.device.mapper.MesDeviceConsumableInfoMapper;
import cn.darkhorse.device.service.MesDeviceConsumableInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangtianqi
 * @since 2024-11-06
 */
@Service
public class MesDeviceConsumableInfoServiceImpl extends ServiceImpl<MesDeviceConsumableInfoMapper, MesDeviceConsumableInfo> implements MesDeviceConsumableInfoService {

    @Override
    public boolean saveConsumableInfo(MesDeviceConsumableInfo mesDeviceConsumableInfo) {
        if (mesDeviceConsumableInfo.getId()==null){
            Long count = lambdaQuery().eq(MesDeviceConsumableInfo::getCode, mesDeviceConsumableInfo.getCode()).count();
            if (count > 0) {
                throw new RuntimeException("编码不可重复");
            }
            mesDeviceConsumableInfo.setCreateBy(SecurityUtils.getUsername());
            mesDeviceConsumableInfo.setCreateTime(LocalDateTime.now());
        }else {
            Long count = lambdaQuery().eq(MesDeviceConsumableInfo::getCode, mesDeviceConsumableInfo.getCode()).ne(MesDeviceConsumableInfo::getId,mesDeviceConsumableInfo.getId()).count();
            if (count > 0) {
                throw new RuntimeException("编码不可重复");
            }
        }
        return saveOrUpdate(mesDeviceConsumableInfo);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public MesDeviceConsumableInfo view(Long id) {
        return getById(id);
    }

    @Override
    public List<MesDeviceConsumableInfo> list(MesDeviceConsumableInfo mesDeviceConsumableInfo) {
        LambdaQueryWrapper<MesDeviceConsumableInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (mesDeviceConsumableInfo.getIntoUseTimeStart()!=null && mesDeviceConsumableInfo.getIntoUseTimeEnd()!=null){
            LocalDate endTime = mesDeviceConsumableInfo.getIntoUseTimeEnd();
            LocalDateTime localDateTime = LocalDateTime.of(endTime, LocalTime.MAX);
            queryWrapper
                    .between(MesDeviceConsumableInfo::getIntoUseTime,
                            mesDeviceConsumableInfo.getIntoUseTimeStart(),
                            localDateTime);
        }
        if (mesDeviceConsumableInfo.getDeviceName()!=null){
            queryWrapper.like(MesDeviceConsumableInfo::getDeviceName,mesDeviceConsumableInfo.getDeviceName());
        }
        if (mesDeviceConsumableInfo.getDeviceCode()!=null){
            queryWrapper.like(MesDeviceConsumableInfo::getDeviceCode,mesDeviceConsumableInfo.getDeviceCode());
        }

        queryWrapper.orderByDesc(MesDeviceConsumableInfo::getIntoUseTime);
        return list(queryWrapper);
    }
}
